Programming With QuickTime Sprites

| Previous | Chapter Contents | Chapter Top |

Sprite Functions

This section describes functions that you use to create and manipulate sprites.

NewSprite

The NewSprite function creates a new sprite in the specified sprite world.

pascal OSErr NewSprite (Sprite *newSprite,
                     SpriteWorld itsSpriteWorld,
                     ImageDescriptionHandle idh,
                     Ptr imageDataPtr,
                     MatrixRecord *matrix,
                     Boolean visible,
                     short layer);
newSprite
Contains a pointer to field that is to receive the new sprite's identifier. On return, this field contains the identifier of the newly created sprite.
itsSpriteWorld
Specifies the sprite world with which the new sprite should be associated.
idh
Contains a handle to an image description of the sprite's image.
imageDataPtr
Contains a pointer to the sprite's image data.
matrix
Contains a pointer to the sprite's matrix. If you pass nil for the matrix parameter, an identity matrix is assigned to the sprite.
visible
Specifies whether the sprite is visible.
layer
Specifies the sprite's layer.

DISCUSSION

You call this function to create a new sprite associated with a sprite world. Once you have created the sprite, you can manipulate it using SetSpriteProperty .

The newSprite , itsSpriteWorld , visible , and layer parameters are required. Sprites with lower layer values appear in front of sprites with higher layer values. If you want to create a sprite that is drawn to the background graphics world, you should specify the constant kBackgroundSpriteLayerNum for the layer parameter.

You can defer assigning image data to the sprite by passing nil for both the idh and imageDataPtr parameters. If you choose to defer assigning image data, you must call SetSpriteProperty to assign the image description handle and image data to the sprite before the next call to SpriteWorldIdle . The caller owns the image description handle and the image data pointer; it is the caller's responsibility to dispose of them after it disposes of a sprite.

RESULT CODES

noErr
0 No error
paramErr
-50 Invalid parameter specified
memFullErr
-108 Not enough memory available

DisposeSprite

The DisposeSprite function disposes of a sprite.

pascal void DisposeSprite (Sprite theSprite);
theSprite
The sprite for this operation.

DISCUSSION

You call this function to dispose of a sprite created by the NewSprite function. The image description handle and image data pointer associated with the sprite are not disposed by this function.

InvalidateSprite

The InvalidateSprite function invalidates the portion of a sprite's sprite world that is occupied by the sprite.

pascal void InvalidateSprite (Sprite theSprite);
theSprite
The sprite for this operation.

DISCUSSION

In most cases, you do not need to call this function. When you call the SetSpriteProperty function to modify a sprite's properties, SetSpriteProperty takes care of invalidating the appropriate regions of the sprite world. However, you might call this function if you change a sprite's image data, but retain the same image data pointer.

SpriteHitTest

The SpriteHitTest function determines whether a location in a sprite's display coordinate system intersects the sprite.

pascal OSErr SpriteHitTest (Sprite theSprite,
                     long flags,
                     Point loc,
                     Boolean *wasHit);
theSprite
Specifies the sprite for this operation.
flags
Specifies flags to control the hit testing operation. These flags are described in "Flags for Sprite Hit Testing" .
loc
Specifies a point in the sprite world's display space to test for the existence of a sprite.
wasHit
Contains a pointer to a Boolean. On return, the value of the Boolean is true if the sprite is at the specified location.

DISCUSSION

You call this function to determine whether a sprite exists at a specified location in the sprite's display coordinate system. This function is useful for hit testing a subset of the sprites in a sprite world and for detecting multiple hits for a single location.

You should apply the sprite world's matrix to the location before passing it to SpriteHitTest . To convert a location to local coordinates, you should use the GlobalToLocal function to convert the location to your window's local coordinate system and then apply the inverse of the sprite world's matrix to the location.

You use the spriteHitTestBounds and spriteHitTestImage flags in the flags parameter to control the hit test operation. Set the spriteHitTestBounds flag to check if there has been a hit anywhere within the sprite's bounding box. Set the spriteHitTestImage flag to check if there has been a hit anywhere within the sprite image.

RESULT CODES

noErr
0 No error
paramErr
-50 Invalid parameter specified

GetSpriteProperty

The GetSpriteProperty function retrieves the value of the specified sprite property.

pascal OSErr GetSpriteProperty (Sprite theSprite,
                     long propertyType,
                     void *propertyValue);
theSprite
Specifies the sprite for this operation.
propertyType
Specifies the property whose value should be retrieved.
propertyValue
A pointer to a variable that will hold the value on return.

DISCUSSION

You call this function to retrieve a value of a sprite property. You set the propertyType parameter to the property you want to retrieve. The following table lists the sprite properties and the data types of the corresponding property values.

Sprite Property

Data Type

kSpritePropertyMatrix
MatrixRecord
kSpritePropertyImageDescription
ImageDescriptionHandle
kSpritePropertyImageDataPtr
Ptr
kSpritePropertyVisible
Boolean
kSpritePropertyLayer
short
kSpritePropertyGraphicsMode
ModifierTrackGraphicsModeRecord

In the case of the kSpritePropertyImageDescription and kSpritePropertyImageDataPtr properties, this function does not return a copy of the data; rather, the pointers returned are references to the sprite's data.

RESULT CODES

noErr
0 No error
invalidSpritePropertyErr
-2065 The specified sprite property does not exist

SetSpriteProperty

The SetSpriteProperty function sets the specified property of a sprite.

pascal OSErr SetSpriteProperty (Sprite theSprite,
                     long propertyType,
                     void *propertyValue);
theSprite
Specifies the sprite for this operation.
propertyType
Specifies the property to be set.
propertyValue
Specifies the new value of the property.

DISCUSSION

You animate a sprite by modifying its properties. You call this function to modify a property of a sprite. This function invalidates the sprite's sprite world as needed.

You set the propertyType parameter to the property you want to modify. Depending on the property type, you set the propertyValue parameter to either a pointer to the property value or the property value itself, cast as a void* .

The following table lists the sprite properties and the data types of the corresponding property values.

Sprite Property

Data Type

kSpritePropertyMatrix
MatrixRecord *
kSpritePropertyImageDescription
ImageDescriptionHandle
kSpritePropertyImageDataPtr
Ptr
kSpritePropertyVisible
Boolean
kSpritePropertyLayer
short
kSpritePropertyGraphicsMode
ModifierTrackGraphicsModeRecord *

RESULT CODES

noErr
0 No error
memFullErr
-108 Not enough memory available
invalidSpritePropertyErr
-2065 Specified sprite property does not exist

© 1998 Apple Computer, Inc.

| Previous | Chapter Contents | Chapter Top |